package cn.engage.basic.controller;

import cn.engage.basic.dto.LoginDto;
import cn.engage.basic.dto.LoginPhoneCodeDto;
import cn.engage.basic.dto.WechatLoginDto;
import cn.engage.basic.service.LoginService;
import cn.engage.basic.util.AjaxResult;
import cn.engage.basic.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/login")
public class loginController {
    @Autowired
    private LoginService loginService;
    // 账号密码登录
    @PostMapping("/logins")
    public AjaxResult login(@RequestBody @Valid LoginDto dto){
        LoginVo loginVo = loginService.login(dto);
        return AjaxResult.me().success(loginVo);
    }
    // 手机验证码登录获取验证码
    @PostMapping("/phoneCode")
    public AjaxResult phoneCode(@RequestBody @Valid LoginPhoneCodeDto dto) {
        loginService.phoneCode(dto);
        return AjaxResult.me().success();
    }
    // 手机验证码登录
    @PostMapping("/loginOnPhone")
    public AjaxResult loginOnPhone(@RequestBody @Valid LoginPhoneCodeDto dto){
        LoginVo loginVo = loginService.loginOnPhone(dto);
        return AjaxResult.me().success(loginVo);
    }

    /**
     *
     * 微信扫描登录第一步
     * 前端扫描二维码后传一个code到后端
     */
    @PostMapping("/wechat")
    public AjaxResult wechatLoginOne(@RequestBody WechatLoginDto dto){
        return loginService.wechatLoginOne(dto);
    }
    /**
     * 微信扫码登录第二步
     * 绑定手机号
     *
     * */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody WechatLoginDto dto){
        LoginVo loginVo = loginService.wechatBinder(dto);
        return AjaxResult.me().success(loginVo);
    }

}
